<!DOCTYPE html>
<html lang='en'>

<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>

<body>
  <!-- 准备好容器 -->
  <div id="test"></div>

  <!--引入react核心库-->
  <script src="../react.js-18/react.development.js" type="text/javascript"></script>
  <!--引入react-dom，用于支持react操作DOM-->
  <script src="../react.js-18/react-dom.development.js" type="text/javascript"></script>
  <!--引入label，用于将jsx转为js-->
  <script src="../react.js-18/babel.min.js" type="text/javascript"></script>
  <!--引入prop-types，用于对组件标签属性的限制-->
  <script src="../react.js-18/prop-types.js" type="text/javascript"></script>

  <script type="text/babel">
    /*
        1.初始化状态：由React.render()触发---初次渲染
          1.constructor()
          2.getDerivedStateFromProps
          3.render()
          4.componentDidMount ====> 常用
        2.更新阶段：有组件内部this.setState()或父组件重新render触发
          1.getDerivedStateFromProps
          2.shouldComponentUpdate()
          3.render()
          4.getSnapshotBeforeUpdate
          5.componentDidUpdate()
        3.卸载组件：由ReactDOM.unmountComponentAtNode()触发
          componentWillUnmount() ====> 常用
    */


    //1.创建组件>
    class Count extends React.Component {
      //构造器
      constructor(props) {
        console.log('Count---constructor');
        super(props)
        //初始化状态
        this.state = { count: 0 }
      }
      //加1
      add = () => {
        //获取原状态
        let { count } = this.state
        //更新状态
        this.setState({ count: count + 1 })
      }
      //卸载组件按钮的回调
      death = () => {
        ReactDOM.unmountComponentAtNode(document.getElementById('test'))
      }
      //强制更新按钮的回调
      force = () => {
        //跳过“阀门”shouldComponentUpdate
        this.forceUpdate()
      }

      //若state的值在任何情况下都取决于props，可以使用getDerivedStateFromProps
      static getDerivedStateFromProps(props, state) {
        console.log('getDerivedStateFromProps', props, state);
        return null
      }

      //在更新之前获取快照
      getSnapshotBeforeUpdate() {
        console.log('getSnapshotBeforeUpdate')
        return 'BZQ'
      }

      //组件挂载完毕的钩子
      componentDidMount() {
        console.log('Count---componentDidMount');
      }

      //组件将要卸载的钩子
      componentWillUnmount() {
        console.log('Count---componentWillUnmount');
      }

      //控制组件更新的“阀门”
      shouldComponentUpdate() {
        console.log('Count---shouldComponentUpdate');
        return true
      }

      //组件更新完毕的钩子
      componentDidUpdate(prevProps, prevState, snapshotValue) {
        console.log('Count---componentDidUpdate', prevProps, prevState, snapshotValue);
      }
      render() {
        console.log('Count---render');
        const { count } = this.state
        return (
          <div>
            <h2>当前求和为：{count}</h2>
            <button onClick={this.add}>点我+1</button>
            <button onClick={this.death}>卸载组件</button>
            <button onClick={this.force}>不更改任何状态中的数据,强制更新一下</button>
          </div>
        )
      }
    }

    //2.渲染组件到页面
    ReactDOM.render(<Count count={199} />, document.getElementById('test'))
  </script>
</body>

</html>